
import exception.ParkingLotFullException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

public class ParkPosManage {
    public String getQueryEmptySql(String parkName){
        return String.format("SELECT id FROM %s WHERE status='empty'", parkName);
    }

    public String getQueryAllSql(String parkName){
        return String.format("SELECT id FROM %s", parkName);
    }

    public String getParkingPos (){
        String[] parkNames = new String[]{"A","B"};
        List<String> emptyPos = new ArrayList<String>();
        for(String parkName: parkNames){
            String sql = getQueryEmptySql(parkName);
            try{
                Connection conn = DatabaseUtil.getConnection();
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                while (rs.next()){
                    emptyPos.add(parkName+rs.getInt("id"));
                }

            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        return getSmallestEmptyPos(emptyPos);
    }

    public String getSmallestEmptyPos(List<String> emptyPos){
        if(emptyPos.isEmpty()){
            return "full";
        } else {
            emptyPos.sort(null);
            return emptyPos.get(0);
        }
    }

    public void parkIntoPos(String Pos, String carInfo){
        String tableName = Pos.split("")[0];
        int id = Integer.parseInt(Pos.split("")[1]);
        String sql = String.format("UPDATE %s SET status='%s' WHERE id=%d", tableName, carInfo, id);
        try{
            Connection conn = DatabaseUtil.getConnection();
            Statement stmt = conn.createStatement();
            stmt.execute(sql);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public String getTicket(String Pos, String carInfo){
        String ticket = Pos.split("")[0]+","+Pos.split("")[1]+","+carInfo;
        return ticket;
    }

    public Boolean ticketJudge(String ticket){
        String[] parkNames = new String[]{"A","B"};
        List<String> parkIds = getParkIds();
        String parkName = ticket.split(",")[0];
        int parkId = Integer.parseInt(ticket.split(",")[1]);
        String parkPos = parkName+parkId;
        String carInfo = ticket.split(",")[2];
        if(!parkIds.contains(parkPos)){
            return false;
        } else {
            try{
                Connection conn = DatabaseUtil.getConnection();
                String sql = String.format("SELECT status FROM %s WHERE id=%d", parkName, parkId);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                String carParked = "";
                while(rs.next()){
                    carParked = rs.getString("status");
                }
                if(!carParked.equals(carInfo)){
                    return false;
                }
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        return true;
    }

    public String fetchCar(String ticket){
        String parkName = ticket.split(",")[0];
        int parkId = Integer.parseInt(ticket.split(",")[1]);
        String carInfo = ticket.split(",")[2];
        String sql = String.format("UPDATE %s SET status='empty' WHERE id=%d", parkName, parkId);
        try{
            Connection conn = DatabaseUtil.getConnection();
            Statement stmt = conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e){
            e.printStackTrace();
        }
        return carInfo;
    }

    public List<String> getParkIds(){
        String[] parkNames = new String[]{"A","B"};
        List<String> parkIds = new ArrayList<>();
        for(String p:parkNames){
            try{
                Connection conn = DatabaseUtil.getConnection();
                String sql = getQueryAllSql(p);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                while (rs.next()){
                    parkIds.add(p+rs.getInt("id"));
                }
            } catch (SQLException e){
                e.printStackTrace();
            }
        }
        return parkIds;
    }
}
